<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区-->
    <el-card>
      <!--添加用户区域-->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="showAddCate">添加用户</el-button>
        </el-col>
      </el-row>
      <!--商品分类区域-->
      <tree-table class="list"
                  :data="cateList"
                  :columns="columns"
                  show-index
                  :selection-type="false"
                  :expand-type="false">
        <!--是否有效模板-->
        <template slot="isOK" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color: lightgreen;"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!--排序-->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="danger" v-else>三级</el-tag>
        </template>
        <!--操作-->
        <template slot="option" slot-scope="scope">
          <el-button class="el-icon-edit" type="primary">编辑</el-button>
          <el-button class="el-icon-delete" type="danger">删除</el-button>
        </template>
      </tree-table>
      <!--//分页功能实现-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userInfo.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="userInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--添加弹出提示框-->
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClose">
      <el-form :model="addCateForm" :rules="addCateRules" ref="ruleAddCateForm" label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!--级联选择器-->
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            expand-trigger="hover"
            :props="cascaderProps"
            @change="parentCateChanged" clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "Cate",
    data() {
      return {
        //商品分类数据
        cateList: [],
        //总条数
        total: 0,
        // 查询条件
        userInfo: {
          type: 3,
          pagenum: 1,
          pagesize: 5
        },
        columns: [
          {
            label: '分类名称',
            prop: 'cat_name'
          },
          {
            //自定义插槽 ---模板列
            label: '是否有效',
            type: 'template',
            template: 'isOK',
          },
          {
            //排序
            label: "排序",
            type: "template",
            template: 'order'
          },
          {
            label: '操作',
            type: 'template',
            template: 'option'
          }
        ],
        addCateDialogVisible: false,
        addCateForm: {
          //需要添加分类的
          cat_name: '',
          cat_level: 0,
          cate_pid: 0
        },
        addCateRules: {
          cat_name: [
            {required: true, message: '请输入分类名称', trigger: 'blur'}
          ]
        },
        //父级分类数据
        parentCateList: [],
        //执行级联选择器配置对象
        cascaderProps: {
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
        },
        // 选中的级联选择器父亲id
        selectedKeys: []
      }
    },
    created() {
      this.getCateLIst()
    },
    methods: {
      async getCateLIst() {
        //发送请求,获取数据
        const {data: res} = await this.$http.get('categories', {params: this.userInfo})
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('获取数据列表失败')
        }
        this.cateList = res.data.result
        this.total = res.data.total
      },
      //  每页显示多少条
      handleSizeChange(newSize) {
        this.userInfo.pagesize = newSize
        this.getCateLIst()
      },
      //当前是第几页
      handleCurrentChange(newNum) {
        this.userInfo.pagenum = newNum
        this.getCateLIst()
      },
      //  添加分类事件
      showAddCate() {

        this.getParentCateList()
        this.addCateDialogVisible = true
        //获取父级分类数据

      },
      //获取父级分类数据
      async getParentCateList() {
        const {data: res} = await this.$http.get('categories', {params: {type: 2}})
        if (res.meta.status !== 200) {
          this.$message.error('获取数据失败')
        }
        console.log(res.data)
        this.parentCateList = res.data
      },
      //添加分类操作
      addCate() {
        this.$refs.ruleAddCateForm.validate(async valid => {
          if (!valid) return
          const {data: res} = await this.$http.post('categories', this.addCateForm)
          console.log(res)
          if (res.meta.status !== 201) {
            return this.$message.error("添加分类失败")
          }
          this.$message.success('添加分类成功')
          //然后刷新页面
          this.getCateLIst()
          //同时让对话框消失
          this.addCateDialogVisible = false
        })

      },

      parentCateChanged() {
        // console.log(selectedKeys)
        // 长度大于0,说明已经选中了父级类名
        if (this.selectedKeys.length > 0) {
          this.addCateForm.cate_pid = this.selectedKeys[this.selectedKeys.length - 1]
          this.addCateForm.cat_level = this.selectedKeys.length
          return
        } else {
          this.addCateForm.cate_pid = 0
          this.addCateForm.cat_level = 0
        }
      },
      //清空表单
      addCateDialogClose() {
        this.$refs.ruleAddCateForm.resetFields()
        this.selectedKeys = []
        this.addCateForm.cate_pid = ''
        this.addCateForm.cate_level = ''
      }
    }

  }
</script>

<style scoped>
  .list {
    margin: 15px 0;
  }
  
  .el-cascader {
    width: 100%;
  }
</style>
